package com.lanju.service.impl;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

/**
 * @description:消息处理
 * @auther: hefeng
 * @creatTime: 2019-8-22 11:34:03
 */
@Service
public class MessageRest{

//    private final static Logger logger = Logger.getLogger(MessageRest.class);


    /**
     * 保存用户发送的文本消息
     * @param map
     * @return
     */
    public void saveMsg(Map<String, String> map){
        if (map != null && map.get("MsgType").equals("text")) {
//            insertMsg(map);
        }
    }

    /**
     * 处理消息，匹配关键字则返回对应模板
     * @param req
     * @param resp
     * @param map
     * @throws IOException
     */
    public void handleMsg(HttpServletRequest req, HttpServletResponse resp, Map<String, String> map) throws IOException {
        if (map.get("EventKey").equals("contact")) {
            map.put("MediaId","saR98JpnunXvfthWot_KxtwJGeRHQ8sFJ3Dos196sFI");
            echoImg(req, resp, map);
        }
    }


    /**
     * 回复空消息
     * @param req
     * @param resp
     * @param map
     * @throws IOException
     */
    public void replyNone(HttpServletRequest req, HttpServletResponse resp,Map<String, String> map) throws IOException {
        map.put("Content", "没有对应关键词的信息");
        echoText(req,resp,map);
    }


    /**
     * 回复文本消息
     * @param req
     * @param resp
     * @param map   replyContent回复的文本消息
     * @throws IOException
     */
    public void echoText(HttpServletRequest req, HttpServletResponse resp, Map<String, String> map) throws IOException {

        PrintWriter out = resp.getWriter();
        String openid = map.get("FromUserName");
        String wechatId = map.get("ToUserName");
        String createTime = String.valueOf(new Date().getTime());
        String Content = map.get("Content");
        //定制回复内容
        String replyXml = "<xml>"
                + "<ToUserName><![CDATA[" + openid + "]]></ToUserName>"//交换ToUserName和FromUserName
                + "<FromUserName><![CDATA[" + wechatId + "]]></FromUserName>"
                + "<CreateTime>" + createTime + "</CreateTime>"//这里可以填创建信息的时间，
                + "<MsgType><![CDATA[text]]></MsgType>"//文本类型text
                + "<Content><![CDATA[" + Content + "]]></Content>"//文本内容，
                + "<MsgId>1234567890123456</MsgId> "//消息id
                + " </xml>";
        out.println(replyXml);//回复
    }

    /**
     * 回复图片消息
     *
     * @param req
     * @param resp
     * @param map   mediaId回复的图片在微信服务器对应的mediaId
     * @throws IOException
     */
    public PrintWriter echoImg(HttpServletRequest req, HttpServletResponse resp, Map<String, String> map) throws IOException {

        PrintWriter out = resp.getWriter();
        String openid = map.get("FromUserName");
        String wechatId = map.get("ToUserName");
        String createTime = String.valueOf(System.currentTimeMillis());
        String MediaId = map.get("MediaId");
        //定制回复内容
        String replyXml = "<xml>"
                + "<ToUserName><![CDATA[" + openid + "]]></ToUserName>"
                + "<FromUserName><![CDATA[" + wechatId + "]]></FromUserName>"
                + "<CreateTime>" + createTime + "</CreateTime>"
                + "<MsgType><![CDATA[image]]></MsgType>"
                + "<Image>"
                + "<MediaId><![CDATA[" + MediaId + "]]></MediaId>"
                + "</Image>"
                + "<MsgId>1234567890123456</MsgId> "
                + " </xml>";
        out.println(replyXml);//回复
		return out;
    }

    /**
     * 回复语音消息
     *
     * @param req
     * @param resp
     * @param map   mediaId回复的图片在微信服务器对应的mediaId
     * @throws IOException
     */
    public void echoVoice(HttpServletRequest req, HttpServletResponse resp, Map<String, String> map) throws IOException {

        PrintWriter out = resp.getWriter();
        String openid = map.get("FromUserName");
        String wechatId = map.get("ToUserName");
        String createTime = String.valueOf(System.currentTimeMillis());

        String MediaId = map.get("MediaId");
        //定制回复内容
        String replyXml = "<xml>"
                + "<ToUserName><![CDATA[" + openid + "]]></ToUserName>"
                + "<FromUserName><![CDATA[" + wechatId + "]]></FromUserName>"
                + "<CreateTime>" + createTime + "</CreateTime>"
                + "<MsgType><![CDATA[voice]]></MsgType>"
                + "<Voice>"
                + "<MediaId><![CDATA[" + MediaId + "]]></MediaId>"
                + "</Voice>"
                + "<MsgId>1234567890123456</MsgId> "
                + " </xml>";
        out.println(replyXml);//回复
    }

    /**
     * 回复视频消息
     *
     * @param req
     * @param resp
     * @param map   mediaId回复的图片在微信服务器对应的mediaId
     * @throws IOException
     */
    public void echoVideo(HttpServletRequest req, HttpServletResponse resp, Map<String, String> map) throws IOException {

        PrintWriter out = resp.getWriter();
        String openid = map.get("FromUserName");
        String wechatId = map.get("ToUserName");
        String createTime = String.valueOf(System.currentTimeMillis());

        String MediaId = map.get("MediaId");
        String Title = map.get("Title");
        String Description = map.get("Description");
        //定制回复内容
        String replyXml = "<xml>\n" +
                "  <ToUserName><![CDATA[" + openid + "]]></ToUserName>\n" +
                "  <FromUserName><![CDATA[" + wechatId + "]]></FromUserName>\n" +
                "  <CreateTime>" + createTime + "</CreateTime>\n" +
                "  <MsgType><![CDATA[video]]></MsgType>\n" +
                "  <Video>\n" +
                "    <MediaId><![CDATA[" + MediaId + "]]></MediaId>\n" +
                "    <Title><![CDATA[" + Title + "]]></Title>\n" +
                "    <Description><![CDATA[" + Description + "]]></Description>\n" +
                "  </Video>\n" +
                "</xml>";
        out.println(replyXml);//回复
    }

    /**
     * 回复图文消息
     *
     * @param req
     * @param resp
     * @param map   mediaId回复的图片在微信服务器对应的mediaId
     * @throws IOException
     */
    public void echoNews(HttpServletRequest req, HttpServletResponse resp, Map<String, String> map) throws IOException {

        PrintWriter out = resp.getWriter();
        String openid = map.get("FromUserName");
        String wechatId = map.get("ToUserName");
        String createTime = String.valueOf(System.currentTimeMillis());

        String Title = map.get("Title");
        String Description = map.get("Description");
        String PicUrl = map.get("PicUrl");
        String Url = map.get("Url");
        //定制回复内容
        String replyXml = "<xml>\n" +
                "  <ToUserName><![CDATA[" + openid + "]]></ToUserName>\n" +
                "  <FromUserName><![CDATA[" + wechatId + "]]></FromUserName>\n" +
                "  <CreateTime>" + createTime + "</CreateTime>\n" +
                "  <MsgType><![CDATA[news]]></MsgType>\n" +
                "  <ArticleCount>1</ArticleCount>\n" +
                "  <Articles>\n" +
                "    <item>\n" +
                "       <Title><![CDATA[" + Title + "]]></Title>\n" +
                "       <Description><![CDATA[" + Description + "]]></Description>\n" +
                "       <PicUrl><![CDATA[" + PicUrl + "]]></PicUrl>\n" +
                "       <Url><![CDATA[" + Url + "]]></Url>\n" +
                "    </item>\n" +
                "  </Articles>\n" +
                "</xml>";
        out.println(replyXml);//回复
    }


}
